package com.hitqz.robot.biz.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.model.vo.patrol.PatrolItemVo;
import com.hitqz.robot.api.common.entity.PatrolItem;
import com.hitqz.robot.api.common.entity.PatrolItemMark;
import com.hitqz.robot.biz.service.PatrolItemMarkService;
import com.hitqz.robot.biz.service.PatrolItemService;
import com.hitqz.robot.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wengyx
 * @date 2024/8/7
 */

@RestController
@AllArgsConstructor
@RequestMapping("/patrolItem")
@Tag(name = "巡检检测项管理")
public class PatrolItemController {

    private final PatrolItemService patrolItemService;

    private final PatrolItemMarkService patrolItemMarkService;


    @GetMapping("/list" )
    @Operation(summary = "查询机器人检测项列表")
    public R<List<PatrolItem>> list() {
        List<PatrolItem> list = patrolItemService.list();
        return R.ok(list);
    }

    @GetMapping("/{id}" )
    @Operation(summary = "根据id查询巡检检测项")
    public R<PatrolItemVo> getById(@PathVariable("id") Integer id) {
        PatrolItemVo vo = patrolItemService.getVoById(id);
        return R.ok(vo);
    }

    @GetMapping("/{id}/mark" )
    @Operation(summary = "根据id查询巡检检测项下标定数据")
    public R<PatrolItemMark> getMark(@PathVariable("id") Integer id) {
        PatrolItemMark result = patrolItemMarkService.getOne(Wrappers.<PatrolItemMark>lambdaQuery().eq(PatrolItemMark::getPatrolItemId, id).last("limit 1"));
        if (result==null){
            return patrolItemMarkService.addPicByPatrolItemId(id);
        }
        return R.ok(result);
    }



    @PostMapping
    @Operation(summary = "新增巡检检测项")
    public R save(@RequestBody @Validated PatrolItem dto) {
        return R.ok(patrolItemService.save(dto));
    }

    @PutMapping
    @Operation(summary = "修改巡检检测项")
    public R updateById(@RequestBody @Validated PatrolItem dto) {
        return R.ok(patrolItemService.updateById(dto));
    }

    @DeleteMapping
    @Operation(summary = "根据id删除巡检检测项")
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(patrolItemService.removeBatchByIds(CollUtil.toList(ids)));
    }

}
